﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPages_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ImageCapCha.ImageUrl = "";
        ImageCapCha.ImageUrl = "./capcha.aspx";
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (tbMaAnToan.Value.ToLower() == Session["capcha"].ToString())
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    //protected void cbAgree_CheckedChanged(object sender, EventArgs e)
    //{
    //    btnTaoTK.Enabled = cbAgree.Checked;

    //    tbMatKhau.Attributes.Add("value", tbMatKhau.Text);
    //    tbXNMatKhau.Attributes.Add("value", tbXNMatKhau.Text);
    //}
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string str = args.Value;
        args.IsValid = true;
        if (str.Equals(""))
            return;
        DateTime dt;
        try
        {
            IFormatProvider format = new System.Globalization.CultureInfo("fr-FR");
            dt = DateTime.Parse(str, format);
        }
        catch
        {
            args.IsValid = false;
        }
    }
    protected void btnTaoTK_Click1(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
           
            ImageCapCha.ImageUrl = "";
            ImageCapCha.ImageUrl = "capcha.aspx";
            DateTime ngaySinh = new DateTime();
            IFormatProvider format = new System.Globalization.CultureInfo("fr-FR");
            if (!tbNgaySinh.Value.Equals(""))
                ngaySinh = DateTime.Parse(tbNgaySinh.Value, format);
            else
                ngaySinh = new DateTime(1990, 1, 1);
            AcountDTO acount = new AcountDTO();
            acount.IDAcount = tbTenDangNhap.Value;
            acount.Pass = tbXNMatKhau.Text;
            acount.FirstName = tbFirstName.Value;
            acount.LastName = tbLastName.Value;
            acount.Male = bool.Parse(rbtnList.SelectedValue);
            acount.HomeTown = int.Parse(ddNoiSinh.SelectedValue);
            acount.Phone = tbMobile.Value;
            acount.Email = tbEmail.Value;
            acount.Website = tbWebsite.Value;
            acount.CMND = tbCMND.Value;
            acount.BirthDay = ngaySinh;
            acount.Role = 1;
            acount.Avarta = tbLinkAvarta.Value;
            acount.BankID = int.Parse(ddNganHang.SelectedValue);
            acount.BankNumber = tbSoTaiKhoan.Value;
            acount.Address = tbAddress.Value;
            acount.Latitude = float.Parse(latInput.Value.Equals("")?"0":latInput.Value);
            acount.Longitude = float.Parse(lngInput.Value.Equals("")?"0" : lngInput.Value);
            if (DBAccess.CreateAcount(acount) > 0)
            {
                //Thu t = new Thu();
                //t.NgayGui = DateTime.Now;
                //t.NguoiGui = "Administrator";
                //t.NguoiNhan = tbTenDangNhap.Value;
                //t.TieuDe = "Thông báo";
                //t.NoiDung = (string)GetGlobalResourceObject("Text", "ChaoMung");
                //t.OpenID = false;
                //t.Email = "";
                //DBAccess.GuiThu(t);
                Session["Acount"] = acount;
            }
        }
        else
            tbMatKhau.Focus();
    }
    protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (-1 == DBAccess.checkAcount(tbTenDangNhap.Value, ""))
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void refreshCapcha(object sender, ImageClickEventArgs e)
    {
        ImageCapCha.ImageUrl = "";
        ImageCapCha.ImageUrl = "capcha.aspx";
    }
    protected void CustomValidator4_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if(!ddNganHang.SelectedValue.Equals("1") && tbSoTaiKhoan.Value.Equals(""))
            args.IsValid = false;
        else
            args.IsValid = true;

    }
    
}